<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category WeChatController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/12/21  17:39
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Index;

use App\Handlers\wechat\MessageHandler;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use EasyWeChat\Kernel\Messages\Article;
use Log;
use EasyWeChat\Factory;

class WeChatController extends Controller
{
    public function index(){
//        $this->returnWxArticle();
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志
        $app = app('wechat.official_account');
        $app->server->push(function($message){
//            Log::info($message['FromUserName']);
//            Log::info($message['MsgType']);
//            return "欢迎关注 hwy！";

            $this->returnWxArticle();
        });
        $app->server->push(MessageHandler::class);
        return $app->server->serve();
    }

    public function getWeChatQr(){
        $app = app('wechat.official_account');
        $result = $app->qrcode->temporary('foo', 600);
        $qrcodeUrl = $app->qrcode->url($result['ticket']);

        $compact = compact('qrcodeUrl');
        return view('index.user.stocks_login',$compact);

    }
    public function returnWxArticle($title = 'a', $author = 'hwy', $content = 'content', $thumb_media_id = '', $digest = '', $source_url = ''){

        $app = app('wechat.official_account');

        $openId = 'ogB-LwbqVudImvoN_hyruvh9qcwQ';

        $text = Carbon::now()->toDateTimeString().'  增加文章【'.$title.'】';

        $app->broadcasting->previewText($text, $openId);
    }

}
{

}
